import { shell } from 'electron'
import EventRoute from '../EventRoute'
import store from '../../store/index';
import { isProcessRunningWin } from '../../utils/index'

const routers: EventRoute[] = []

// 显示加群窗口
routers.push(
  new EventRoute('create-discussion-group-window', (api) => {
    const position = api.aiIconWindow.getPosition()
    api.discussionGroupWindow.onDisplay(position[0], position[1])
    api.aiIconWindow.frame.webContents.send('create-discussion-group')
  })
)

// 隐藏加群窗口
routers.push(
  new EventRoute('destroy-discussion-group-window', (api) => {
    if (api.discussionGroupWindow.frame !== null && !api.discussionGroupWindow.frame.isDestroyed()) {
      api.discussionGroupWindow.hide()
    }
    api.aiIconWindow.frame.webContents.send('destroy-discussion-group')
  })
)

// 拖拽ai图标窗口
// 开始拖拽
routers.push(
  new EventRoute('aiIcon-window-start-drag', (api) => {
    api.aiIconWindow.startDrag()
  })
)
// 移动拖拽
routers.push(
  new EventRoute('aiIcon-window-move-drag', (api) => {
    api.aiIconWindow.moveDrag()
  })
)
// 结束拖拽
routers.push(
  new EventRoute('aiIcon-window-stop-drag', (api) => {
    api.aiIconWindow.stopDrag()
  })
)

// 设置鼠标事件是否穿透
let isIgnoringMouseEvents = false;
routers.push(
  new EventRoute('set-ignore-mouse-events-aiIcon-window', (api, { data }) => {
    if (data !== isIgnoringMouseEvents) {
      api.aiIconWindow.setIgnoreMouseEvents(data)
      isIgnoringMouseEvents = data
    }
  })
)

// 显示ai助手
routers.push(
  new EventRoute('show-AiAssistant-window', (api) => {
    api.aiIconWindow.show()
  })
)

// 隐藏ai助手需要通知客户端修改状态
routers.push(
  new EventRoute('hide-AiAssistant-window', (api) => {
    // const appName = 'lightbox-next' // lightbox
    const appName = store.get('appName') || 'lightbox'
    const url = `${appName}://msgtype==hideAiassistant&&show==true`;
    isProcessRunningWin(appName + '.exe').then(running => {
      if (running) {
        console.log('已启动')
        shell.openExternal(url);
      } else  {
        console.log('未启动')
      }
    });
    api.aiIconWindow.hide()
  })
)

// 打开lightbox
routers.push(
  new EventRoute('open-lightbox', (_api) => {
    // const appName = 'lightbox-next' // lightbox
    const appName = store.get('appName') || 'lightbox-next'
    const url = `${appName}://msgtype==showMainWindow&&show==true`;
    isProcessRunningWin(appName + '.exe').then(running => {
      if (running) {
        console.log('已启动')
        shell.openExternal(url);
      } else  {
        console.log('未启动')
      }
    });
  })
)

// 触发截图
routers.push(
  new EventRoute('trigger-screenshot', async (api) => {
    api.aiIconWindow.triggerScreenshot()
  })
)

// 显示更多功能
routers.push(
  new EventRoute('show-more-features', async (api) => {
    api.moreFeaturesWindow.showMoreFeatures()
  })
)

// 隐藏更多功能
routers.push(
  new EventRoute('hide-more-features', async (api, { data }) => {
    api.moreFeaturesWindow.hideMoreFeatures(data)
  })
)

// 显示右键菜单
routers.push(
  new EventRoute('show-right-click-menu', async (api) => {
    api.rightClickMenuWindow.showRightClickMenu()
  })
)

// 隐藏右键菜单
routers.push(
  new EventRoute('hide-right-click-menu', async (api, { data }) => {
    api.rightClickMenuWindow.hideRightClickMenu(data)
  })
)

// 获得焦点
routers.push(
  new EventRoute('get-focus', async (api) => {
    api.aiIconWindow.focus()
  })
)

export default routers
